﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;

namespace Common.TextMatch
{
    /// <summary>
    /// 
    /// </summary>
    public class RegexValidator : Validator
    {

#if BOOST_STARTUP_PERFORMANCE
		private const RegexOptions regexOptions = RegexOptions.None;	
#else
        private const RegexOptions regexOptions = RegexOptions.Compiled;
#endif

        private Regex regularExpression;
        private string pattern;

        //Ensure is created with a pattern...
        private RegexValidator() { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pattern"></param>
        public RegexValidator(string pattern)
        {
            Debug.Assert(pattern != null, "Unexpected null value.", "The RegexValidator.ctor() method requires a value.");

            if (pattern == null)
                throw new ArgumentNullException("pattern");

            this.pattern = pattern;
            this.regularExpression = new Regex(this.pattern, regexOptions);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public override bool Validate(string value)
        {
            Debug.Assert(value != null, "Unexpected null value.", "The RegexValidator.Validate() method requires a value.");

            if (value == null)
                throw new ArgumentNullException("value");

            return NormalizeValidation(this.regularExpression.IsMatch(value));
        }
    }
}
